package com.ibp.controller;

import java.util.HashMap;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ibp.entity.HostMgr;
import com.ibp.entity.NodeMgr;
import com.ibp.response.DataResponse;
import com.ibp.response.FormResponse;
import com.ibp.response.SimpleResponse;
import com.ibp.service.HostMgrService;
import com.ibp.service.NodeMgrService;
import com.ibp.service.impl.HostMgrServiceImpl;
import com.ibp.utils.StringUtil;

@Controller
@RequestMapping("nodeMgr")
public class NodeMgrController {
	
	private static Logger logger = LoggerFactory
			.getLogger(NodeMgrController.class);
	
	
	@Autowired 
	NodeMgrService nodeMgrServiceImpl;
	@Autowired 
	HostMgrService hostMgrServiceImpl;

	
	/**  																				  
	 * 删除 node 																			      
	 * @param node  															  
	 */  																				  
	@ResponseBody  																		  
	@RequestMapping("/deleteNode") 															  
	public SimpleResponse<String> deleteNode(NodeMgr nodeMgr){  					  
		logger.info("删除node-->"+nodeMgr.toString());  							  
		SimpleResponse<String> response = new SimpleResponse<String>();  		
		response.setData(nodeMgrServiceImpl.deleteNode(nodeMgr));	
		response.setSuccess(true);  													  
		return response;  																  
	}
	
	/***  																				  
	 * 修改 																			      
	 * @param node  															  
	 */  																				  
	@ResponseBody  																		  
	@RequestMapping("/updateNode")  														  
	public FormResponse<String> update(NodeMgr nodeMgr){  					  
		logger.info("修改node-->"+nodeMgr.toString());  							  
		FormResponse<String> response = new FormResponse<String>();  	
		response.setData(nodeMgrServiceImpl.updateNode(nodeMgr) 	);	
		response.setMessage("修改失败！");  											  
		 																				  
		return response;  																  
	} 
	
	/*** 																	              
	 * 新增 																			      
	 * @param node 															  
	 */  																				  
	@ResponseBody  																		  
	@RequestMapping("/addNode")  															  
	public FormResponse<String> add(NodeMgr nodeMgr){  					  
		logger.info("新增node-->"+nodeMgr.toString());  		
		FormResponse<String> response = new FormResponse<String>();  	
		String respMsg = nodeMgrServiceImpl.addNode(nodeMgr);
		response.setData(respMsg);
		return response;  															      
	} 
	
	/*** 																	              
	 * 查询node列表 																			      
	 * @param node 															  
	 */
	@RequestMapping({"/getNodeMgrList"})
	  @ResponseBody
	  public DataResponse<List<NodeMgr>> getServiceNodes(NodeMgr node)
	  {
	    logger.info("交易码查询" + node.toString());
	    DataResponse<List<NodeMgr>> response = new DataResponse<List<NodeMgr>>();
	    response.setData(nodeMgrServiceImpl.getNodeMgrList(node));
	    return response;
	  }
	
	/*** 																	              
	 * 查询node列表 																			      
	 * @param node 															  
	 */
	@RequestMapping({"/getHostMgrList"})
	  @ResponseBody
	  public DataResponse<List<HostMgr>> getServiceHosts(NodeMgr node)
	  {
		 logger.info("交易码查询" + node.toString());
	    DataResponse<List<HostMgr>> response = new DataResponse<List<HostMgr>>();
	    List<NodeMgr> list = nodeMgrServiceImpl.getNodeMgrList(node);
	    List<HostMgr> listHost = null;
	    if(list.get(0).getHosts()!=null){
	    	 listHost = HostMgrServiceImpl.getHostList(list.get(0).getHosts(),list.get(0).getNode());
	    }
	    
	    response.setData(listHost);
	    return response;
	  }
	
	/**  																				  
	 * 删除 host 																			      
	 * @param host  															  
	 */  																				  
	@ResponseBody  																		  
	@RequestMapping("/deleteHost") 															  
	public SimpleResponse<String> deleteHost(HostMgr hostMgr){  					  
		logger.info("删除host-->"+hostMgr.toString());  							  
		SimpleResponse<String> response = new SimpleResponse<String>();  		
		response.setData(hostMgrServiceImpl.deleteHost(hostMgr));	
		response.setSuccess(true);  													  
		return response;  																  
	}
	
	/*** 																	              
	 * 新增 																			      
	 * @param node 															  
	 */  																				  
	@ResponseBody  																		  
	@RequestMapping("/addHost")  															  
	public FormResponse<String> addHost(HostMgr hostMgr){  					  
		logger.info("新增host-->"+hostMgr.toString());  	
		FormResponse<String> response = new FormResponse<String>();  
		response.setData(hostMgrServiceImpl.addHost(hostMgr));	
		System.out.println(response.getData());
		return response;  															      
	} 
	
}
